package com.example.dw.admin.controller;

import com.example.dw.admin.dto.CreateCourseDTO;
import com.example.dw.admin.dto.PageRequestDTO;
import com.example.dw.admin.dto.UpdateCourseDTO;
import com.example.dw.admin.service.AdminCourseService;
import com.example.dw.common.Result;
import com.example.dw.entity.Course;
import com.example.dw.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理端课程管理控制器
 * @author system
 * @since 2024-01-01
 */
@Tag(name = "管理端课程管理", description = "课程信息管理接口")
@Slf4j
@RestController
@RequestMapping("/admin")
@Validated
public class AdminCourseController {

    @Autowired
    private AdminCourseService adminCourseService;

    /**
     * 获取部分课程信息
     */
    @Operation(summary = "获取部分课程信息", description = "获取指定数量的最新课程")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/courseSome/{count}")
    public Result<?> getSomeCourses(@Parameter(description = "获取课程数量") @PathVariable Integer count) {
        try {
            List<Course> courses = adminCourseService.getSomeCourses(count);
            return Result.success("获取课程列表成功", courses);
        } catch (Exception e) {
            log.error("获取部分课程失败: {}", e.getMessage());
            return Result.error("获取课程列表失败");
        }
    }

    /**
     * 根据学校ID获取课程
     */
    @Operation(summary = "根据学校ID获取课程", description = "获取指定学校的所有课程")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/course/school/{id}")
    public Result<?> getCoursesBySchool(@Parameter(description = "学校ID") @PathVariable Integer id) {
        try {
            List<Course> courses = adminCourseService.getCoursesBySchool(id);
            return Result.success("获取学校课程成功", courses);
        } catch (Exception e) {
            log.error("根据学校ID获取课程失败: {}", e.getMessage());
            return Result.error("获取学校课程失败");
        }
    }

    /**
     * 获取所有课程信息
     */
    @Operation(summary = "获取所有课程信息", description = "获取系统中的所有课程")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/courseAll")
    public Result<?> getAllCourses() {
        try {
            List<Course> courses = adminCourseService.getAllCourses();
            return Result.success("获取所有课程成功", courses);
        } catch (Exception e) {
            log.error("获取所有课程失败: {}", e.getMessage());
            return Result.error("获取所有课程失败");
        }
    }

    /**
     * 分页获取课程列表
     */
    @Operation(summary = "分页获取课程列表", description = "分页查询课程信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @PostMapping("/course/page")
    public Result<?> getCoursesByPage(@Parameter(description = "分页查询参数") @RequestBody @Validated PageRequestDTO pageRequest) {
        try {
            Map<String, Object> result = adminCourseService.getCoursesByPage(pageRequest);
            return Result.success("分页获取课程成功", result);
        } catch (Exception e) {
            log.error("分页获取课程失败: {}", e.getMessage());
            return Result.error("分页获取课程失败");
        }
    }

    /**
     * 获取课程详情
     */
    @Operation(summary = "获取课程详情", description = "根据课程ID获取课程详细信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "课程不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/courseE/{courseid}")
    public Result<?> getCourseById(@Parameter(description = "课程ID") @PathVariable Integer courseid) {
        try {
            Course course = adminCourseService.getCourseById(courseid);
            return Result.success("获取课程详情成功", course);
        } catch (RuntimeException e) {
            log.error("获取课程详情失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("获取课程详情失败: {}", e.getMessage());
            return Result.error("获取课程详情失败");
        }
    }

    /**
     * 创建课程
     */
    @Operation(summary = "创建课程", description = "创建新的课程")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "参数验证失败"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @PostMapping("/course")
    public Result<?> createCourse(@Parameter(description = "创建课程参数") @RequestBody @Validated CreateCourseDTO createCourseDTO) {
        try {
            adminCourseService.createCourse(createCourseDTO);
            return Result.success("创建课程成功");
        } catch (RuntimeException e) {
            log.error("创建课程失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("创建课程失败: {}", e.getMessage());
            return Result.error("创建课程失败");
        }
    }

    /**
     * 更新课程信息
     */
    @Operation(summary = "更新课程信息", description = "更新现有课程的信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "参数验证失败"),
            @ApiResponse(responseCode = "404", description = "课程不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @PutMapping("/course")
    public Result<?> updateCourse(@Parameter(description = "更新课程参数") @RequestBody @Validated UpdateCourseDTO updateCourseDTO) {
        try {
            adminCourseService.updateCourse(updateCourseDTO);
            return Result.success("更新课程成功");
        } catch (RuntimeException e) {
            log.error("更新课程失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("更新课程失败: {}", e.getMessage());
            return Result.error("更新课程失败");
        }
    }

    /**
     * 删除课程
     */
    @Operation(summary = "删除课程", description = "删除指定的课程")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "404", description = "课程不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @DeleteMapping("/course")
    public Result<?> deleteCourse(@Parameter(description = "课程ID") @RequestParam Integer courseid) {
        try {
            adminCourseService.deleteCourse(courseid);
            return Result.success("删除课程成功");
        } catch (RuntimeException e) {
            log.error("删除课程失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("删除课程失败: {}", e.getMessage());
            return Result.error("删除课程失败");
        }
    }

    /**
     * 获取全部教师
     */
    @Operation(summary = "获取全部教师", description = "获取系统中所有的教师用户")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/tercherAll")
    public Result<?> getAllTeachers() {
        try {
            List<UserVO> teachers = adminCourseService.getAllTeachers();
            return Result.success("获取教师列表成功", teachers);
        } catch (Exception e) {
            log.error("获取教师列表失败: {}", e.getMessage());
            return Result.error("获取教师列表失败");
        }
    }
} 